home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Games / dynAMIte / Developer / C / dynbot.c < prev   
C/C++ Source or Header  |  2001-07-22  |  2KB  |  130 lines

  1. #include    <stdlib.h>
  2. #include     <stdio.h>
  3. #include    <time.h>
  4.  
  5. #include     <exec/semaphores.h>
  6. #include     <dos/dos.h>
  7. #include     <proto/exec.h>
  8. #include     <proto/dos.h>
  9.  
  10. #include     "dynamite.h"
  11.  
  12.  
  13. int
  14. main()
  15. {
  16.     struct dynamitesemaphore    *dynasema;
  17.     struct player                 *ourplayer;
  18.     BOOL                        done = FALSE,
  19.                                 delay;
  20.  
  21.   LONG thisbotnum;
  22.  
  23.     /* check if dynamite is running */
  24.  
  25.     srand (time(NULL));
  26.  
  27.     Forbid();    /* try to find the semaphore */
  28.     dynasema=(struct dynamitesemaphore *)FindSemaphore("dynAMIte.0");
  29.     if(dynasema)
  30.     {
  31.         /*
  32.         increase opencount to tell dynamite that you are using the
  33.         semaphore. dynamite will remove semaphore only if opencnt is
  34.         0 at its end
  35.         */
  36.  
  37.         dynasema->opencnt++;
  38.  
  39.     thisbotnum=dynasema->opencnt;
  40.  
  41.     /* set botinfo string */
  42.     dynasema->botinfo[thisbotnum]=(char*)"dynamite sample bot v1.0 - doing nothing useful"
  43.  
  44.         printf("Clients using the semaphore: %ld\n",dynasema->opencnt);
  45.     }
  46.     Permit();
  47.  
  48.     if(dynasema)
  49.         {
  50.         printf("dynAMIte is started\n");
  51.  
  52.         while (!done)
  53.           {
  54.             if(CheckSignal(SIGBREAKF_CTRL_C))
  55.                 {
  56.                 done=TRUE;
  57.                 }
  58.             else
  59.                 {
  60.                 delay=TRUE;
  61.                 ObtainSemaphore(&dynasema->sema);
  62.  
  63.                 if(dynasema->quit)
  64.                     {
  65.                     /* dynamite wants to quit, so we do dynamite a favour */
  66.                     printf("dynAMIte is about to quit...\n");
  67.                     done=TRUE;
  68.                     }
  69.                 else
  70.                     {
  71.                     /* if a game is running */
  72.                     if(dynasema->gamerunning>=GAME_GAME)
  73.                         {
  74.                         /* and player is no observer */
  75.                         if(dynasema->thisplayer<8)
  76.                             {
  77.                             delay=FALSE;
  78.  
  79.                             ourplayer=dynasema->players[dynasema->thisplayer];
  80.  
  81.                             /* and our player is alive */
  82.                             if(ourplayer->dead)
  83.                                 {
  84.                                 /* do your AI stuff */
  85.                                 dynasema->walk= rand()%DIR_UP+1;
  86.                                 }
  87.                             }
  88.                         }
  89.  
  90.                   }
  91.  
  92.                 ReleaseSemaphore(&dynasema->sema);
  93.  
  94.                 if(delay)
  95.                     {
  96.                     /* no game is running */
  97.                     /* do a small delay to let the cpu do other things :( */
  98.                     Delay(10);
  99.                     }
  100.  
  101.             }
  102.         }
  103.  
  104.         ObtainSemaphore(&dynasema->sema);
  105.  
  106.     /* reset direction */
  107.     dynasema->walk=DIR_NONE;
  108.  
  109.     /* set botinfo entry back to 0 */
  110.     dynasema->botinfo[thisbotnum]=NULL;
  111.  
  112.         /*
  113.         decrease opencount to tell dynamite that you no longer need
  114.        the semaphore.
  115.        dynamite will remove the semaphore only if opencnt is
  116.        0 at the end
  117.         */
  118.  
  119.         dynasema->opencnt--;
  120.         ReleaseSemaphore(&dynasema->sema);
  121.         }
  122.     else
  123.         {
  124.         printf("dynamite is not running\n");
  125.         }
  126.  
  127.  
  128.     return EXIT_SUCCESS;
  129. }
  130.